﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Configuration;

namespace Synergy
{
    static class pub
    {
    //      static  Lujn.ServiceReference1.Service1SoapClient sv = new Lujn.ServiceReference1.Service1SoapClient();
    //      //[DllImport("User32.dll", CharSet = CharSet.Auto)]
    //      //public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        static DataTable 人员权限清单;

        public static DataTable 权限清单
        {
            get { return 人员权限清单; }
            set { 人员权限清单 = value; }
        }
        public static string adminname()
        {

            return "admin";

        }
            //public static PrintDoc.WebReference.HzwlService db = new PrintDoc.WebReference.HzwlService();
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        public static string token = "hofgiadugansfoiuqtDSIOFASMFLKA79641654679&%JFM-JFASOIUnozijzljga";
        public static string LoginUser;
        public static int ToNum(string s)
        {

            try
            {
                return int.Parse(s);

            }
            catch
            {
                return 0;
            }

        }
        public static decimal ToDecimal(string s)
        {
           
            try
            {
                return decimal.Parse(s);

            }
            catch
            {
                return 0;
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql">语句</param>
        /// <param name="message">描述</param>
        /// <returns></returns>
        public static int ExcuteSql(string sql, string message)
        {
            string logsql;
            logsql = sql.Replace("'", "").Replace("-", "");
            string esql;
            esql = "insert into z_log(执行语句,描述,时间,人员,MAC)";
            esql += " values('" + logsql + "','"+ message  +"',getdate(),'"+ pub.LoginUser +"','"+ pub.mac +"')";
            SqlHelper.SqlHelper.ExecuteNonQuery(SqlHelper.SqlHelper.U8conn, CommandType.Text, esql);
           return  SqlHelper.SqlHelper.ExecuteNonQuery(SqlHelper.SqlHelper.U8conn, CommandType.Text, sql);
        }
        public static DataTable  U8GetTable(string sql)
        {
            //正式环境
            SqlHelper.SqlHelper.U8conn = ConfigurationManager.ConnectionStrings["actived"].ToString();;

            try
           {
               return SqlHelper.SqlHelper.ExecuteDataset(SqlHelper.SqlHelper.U8conn, CommandType.Text, sql).Tables[0];
           }
           catch {
               return null;
           }
        }
        public static DataTable SysGetTable(string sql)
        {
            //正式环境
            SqlHelper.SqlHelper.Sysconn = ConfigurationManager.ConnectionStrings["actived"].ToString();
            //SqlHelper.SqlHelper.Sysconn = "Data Source = 120.27.210.156; DataBase = UFDATA_901_2020; uid = sa; pwd = 3edc$RFV5tgb^YHN";

            return SqlHelper.SqlHelper.ExecuteDataset(SqlHelper.SqlHelper.Sysconn, CommandType.Text, sql).Tables[0];
        }
        public static string GetCode128B(string str)
        {
            if (str.Length > 0)
            {
                
                int i;
                int checksum;
                int i_tmp;
                string checkCode;
                checkCode = "";
                checksum = 104;
                for (i = 1; i <= str.Length; i++)
                {
                    i_tmp = Asc(str.Substring(i-1, 1));
                    //i_tmp = Convert.ToInt32(str.Substring(i - 1, 1));
                    if (i_tmp >= 32)
                    {
                        checksum = checksum + (i_tmp - 32) * i;
                    }
                    else
                    {
                        checksum = checksum + (i_tmp + 64) * i;
                    }
                    Console.WriteLine(checksum);
                }
               
                checksum = checksum % 103;
                if (checksum < 95)
                {
                    checksum = checksum + 32;
                }
                else
                {
                    checksum = checksum + 100;
                }
                checkCode = Convert.ToChar(checksum).ToString();
                return Convert.ToChar(204).ToString() + str + checkCode + Convert.ToChar(206).ToString();
            }
            else
            {
                return "";
            }
        }
        public static int Asc(string character)
        {
            if (character.Length == 1)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
                return (intAsciiCode);
            }
            else
            {
                throw new Exception("Character is not valid.");
            }

        }
        public static string Chr(int asciiCode)
        {
            if (asciiCode >= 0 && asciiCode <= 255)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                byte[] byteArray = new byte[] { (byte)asciiCode };
                string strCharacter = asciiEncoding.GetString(byteArray);
                return (strCharacter);
            }
            else
            {
                throw new Exception("ASCII Code is not valid.");
            }
        }




        internal static string Get开工日期by生产订单号ID(string 生产订单号ID  )
        {
            try
            {
                string sql;
                sql = " select min(startdate) from mom_morder where moid ='" + 生产订单号ID + "'";
                DataTable tb = pub.U8GetTable(sql);
                if (tb != null)
                {
                    return DateTime.Parse(tb.Rows[0][0].ToString()).ToString("yyyy-MM-dd");
                }
                else
                {
                    return "";
                }
            }
            catch
            {
                return "";
            }
        }

        internal static object Get完工日期by生产订单号ID(string 生产订单号ID)
        {
            try
            {
                string sql;
                sql = " select max(duedate) from mom_morder where moid ='" + 生产订单号ID + "'";
                DataTable tb = pub.U8GetTable(sql);
                if (tb != null)
                {
                    return DateTime.Parse( tb.Rows[0][0].ToString()).ToString("yyyy-MM-dd");
                }
                else
                {
                    return "";
                }
            }
            catch
            {
                return "";
            }
        }

        internal static void kill(BarTender.Application ba)
        {
            System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(ba.ProcessId);   //得到对进程k的引用
            p.Kill();     //关闭进程k
        }

       
       

        public static string mac { get; set; }
        public static  bool 解析条码(string tm, out string 生产订单号, out string 生产订单行号, out string 物料编号, out decimal 数量, out string 加工顺序, out string 工序编号)
        {
            string s;
            int p1;
            int p2;
            int p3;
            int p4;
            int p5;
            int p6;
            int p7;
            int len = tm.Length;
            生产订单号 = "";
            生产订单行号 = "";
            物料编号 = "";
            数量 = 0;
            加工顺序 = "";
            工序编号 = "";

            try
            {
                //生产订单号$生产订单行号$物料编号$数量$加工顺序$工序编号$工序名称
                if (len > 0)
                {
                    p1 = tm.IndexOf("$", 0);
                    生产订单号 = tm.Substring(0, p1);
                    p2 = tm.IndexOf("$", p1 + 1);
                    生产订单行号 = tm.Substring(p1 + 1, p2 - p1 - 1);
                    p3 = tm.IndexOf("$", p2 + 1);
                    物料编号 = tm.Substring(p2 + 1, p3 - p2 - 1);
                    p4 = tm.IndexOf("$", p3 + 1);
                    数量 = decimal.Parse(tm.Substring(p3 + 1, p4 - p3 - 1));
                    p5 = tm.IndexOf("$", p4 + 1);
                    加工顺序 = tm.Substring(p4 + 1, p5 - p4 - 1);
                    工序编号 = tm.Substring(p5 + 1);



                }
                if (生产订单号 + 生产订单行号 + 物料编号 + 加工顺序 + 工序编号 != "")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                生产订单号 = "";
                生产订单行号 = "";
                物料编号 = "";
                数量 = 0;
                加工顺序 = "";
                工序编号 = "";

                return false;
            }
        }
        public static bool 解析条码(string tm, out string 生产订单号, out string 生产订单行号, out string 物料编号, out decimal 数量, out string 加工顺序, out string 工序编号, out long 派工id, out decimal 派工数量)
        {
            string s;
            int p1;
            int p2;
            int p3;
            int p4;
            int p5;
            int p6;
            int p7;
            int len = tm.Length;
            生产订单号 = "";
            生产订单行号 = "";
            物料编号 = "";
            数量 = 0;
            加工顺序 = "";
            工序编号 = "";
            派工id = 0;
            派工数量 = 0;
            string[] code = tm.Split('$');
            try
            {
                //生产订单号$生产订单行号$物料编号$数量$加工顺序$工序编号$工序名称
                if (code.Length > 0)
                {
                    生产订单号 = code[0];
                    生产订单行号 = code[1];
                    物料编号 = code[2];
                    数量 = decimal.Parse(code[3]);
                    加工顺序 = code[4];
                    工序编号 = code[5];
                    派工id = long.Parse(code[6]);
                    派工数量 = decimal.Parse(code[7]);
                    //p1 = tm.IndexOf("$", 0);
                    //生产订单号 = tm.Substring(0, p1);
                    //p2 = tm.IndexOf("$", p1 + 1);
                    //生产订单行号 = tm.Substring(p1 + 1, p2 - p1 - 1);
                    //p3 = tm.IndexOf("$", p2 + 1);
                    //物料编号 = tm.Substring(p2 + 1, p3 - p2 - 1);
                    //p4 = tm.IndexOf("$", p3 + 1);
                    //数量 = decimal.Parse(tm.Substring(p3 + 1, p4 - p3 - 1));
                    //p5 = tm.IndexOf("$", p4 + 1);
                    //加工顺序 = tm.Substring(p4 + 1, p5 - p4 - 1);
                    //工序编号 = tm.Substring(p5 + 1);



                }
                if (生产订单号 + 生产订单行号 + 物料编号 + 加工顺序 + 工序编号 != "")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                生产订单号 = "";
                生产订单行号 = "";
                物料编号 = "";
                数量 = 0;
                加工顺序 = "";
                工序编号 = "";

                return false;
            }
        }
        public static string Get是否已检验(string 报工id)
        {
            string sql;
            sql = "select * from z_zhijian where baogongid='" + 报工id + "'";
            DataTable tb = pub.U8GetTable(sql);
            if (tb.Rows.Count > 0)
            {
                return "已检验";
            }
            else
            {
                return "未检验";
            }
        }
        public static void 人员权限表()
        {
            string sql = "select 权限编号  from Z_usergn where   授权人员='" + pub.LoginUser + "'";
            权限清单 = pub.U8GetTable(sql);
        }
        public static bool 是否有权限(string 权限编号  )
        {
            return true;
            if (LoginUser==pub.adminname())
            {
                return true;
            }
            string sql;
            sql = $"select 1 from Z_gn where 权限编号 = '{权限编号}'";
            DataTable dt = pub.U8GetTable(sql);
            if (dt == null || dt.Rows.Count <= 0)
            {
                return true;
            }
            sql = "select * from Z_usergn where 权限编号='" + 权限编号 + "' and 授权人员 ='" + LoginUser + "'";
            DataTable tb = pub.U8GetTable(sql);
            if (tb.Rows.Count >0)
            {
                return true;
            }
            else
            {
                return false;
                //return true;
            }
         
            
        }

        internal static string Get出库单号(string 报工id)
        {
            string sql;
            sql = "select cCode,cDefine1 from rdrecord11";
            sql += "  where cDefine1 ='" + 报工id + "'";
            DataTable tb3 = pub.U8GetTable(sql);
            if (tb3.Rows.Count > 0)
            {
                return tb3.Rows[0][0].ToString();
            }
            else
            {
                return "";
            }
        }

        internal static string Get入库单号(string 报工id,string 生产订单号)
        {
            string sql;
            sql = "select rdrecord10.ccode from rdrecord10 join rdrecords10 on rdrecord10.ID =rdrecords10.ID ";
            sql += " where cdefine31 ='" + 报工id + "'"+ "and rdrecord10.cMPoCode ='"+ 生产订单号 + "'";
            DataTable tb3 = pub.U8GetTable(sql);
            if (tb3.Rows.Count > 0)
            {
                string code = "";
                for (int i = 0; i < tb3.Rows.Count; i++)
                {
                    code += tb3.Rows[i][0].ToString();
                }
                return code;
            }
            else
            {
                return "";
            }
        }

        public static string 根据老物流号获取新料号(string 生产订单号, string 行号)
        {
            string sql = $"select 物料编号 from View_生产订单明细 where 生产订单号 = '{生产订单号}' and 行号 = '{行号}'";
            return SqlSugarHelp.scope.Ado.GetString(sql);
        }
      
    }
}
